package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckgroupMapper;
import com.itheima.health.dao.CheckitemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
@Slf4j
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;
    
    /**
     * @Author: 15041
     * @Date: 12:10 2021/1/6
     * @Parms: [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     *Description: 分页查询检查组
    */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("检查组-分页查询serviceImpl层方法传参："+queryPageBean);
        Page<Object> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> list = checkgroupMapper.findByPage(queryPageBean.getQueryString());
        //创建返回值对象
        PageResult pageResult = new PageResult();
        //将总条数传入
        pageResult.setTotal(page.getTotal());
        //传入查询到的数据
        pageResult.setRows(list);
        return pageResult;
    }
    
    /**
     * @Author: 15041
     * @Date: 12:10 2021/1/6
     * @Parms: []
     * @ReturnType: java.util.List<com.itheima.health.pojo.CheckItem>
     *Description: 查询所有检查项
    */
    @Override
    public List<CheckItem> findCheckitem() {
        log.info("检查组-查询所有检查项serviceImpl层方法执行了");
        return checkitemMapper.selectAll();
    }

    /**
     * @Author: 15041
     * @Date: 12:10 2021/1/6
     * @Parms: [checkGroup, checkitemIds]
     * @ReturnType: void
     *Description: 新增检查组
    */
    @Override
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        log.info("检查组-新增检查项serviceImpl层方法传参："+checkGroup+" and "+checkitemIds);
        checkgroupMapper.insert(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            checkgroupMapper.adds(checkGroup.getId(),checkitemId);
        }

    }
    
    /**
     * @Author: 15041
     * @Date: 12:11 2021/1/6
     * @Parms: [id]
     * @ReturnType: com.itheima.health.pojo.CheckGroup
     *Description: 根据id查询检查项
    */
    @Override
    public CheckGroup findCheckIds(Integer id) {
        log.info("检查组-根据id查询检查项serviceImpl层方法传参："+id);
        return checkgroupMapper.findCheckIds(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public void update(CheckGroup checkGroup,Integer[] checkitemIds) {
        log.info("检查组-修改检查组serviceImpl层方法传参："+checkGroup+"id:"+checkitemIds);
        //修改检查组数据
        checkgroupMapper.updateByPrimaryKeySelective(checkGroup);
        //创建检查组和检查项的关联表对象
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        //传入检查组id
        checkGroupItem.setCheckgroupId(checkGroup.getId());
        //根据检查组id删除中间表中的检查组
        int delete = checkGroupItemMapper.delete(checkGroupItem);
        log.info("删除了"+delete);
        //遍历传过来的检查项id
        for (Integer checkitemId : checkitemIds) {
            //创建中间表对象
            CheckGroupItem groupItem = new CheckGroupItem();
            //传入检查项id
            groupItem.setCheckitemId(checkitemId);
            checkgroupMapper.adds(checkGroup.getId(),checkitemId);
        }
    }

    @Override
    public List<CheckGroup> findGroupAll() {
        log.info("检查组-查询所有查到的内容：{}",checkgroupMapper.selectAll());
        return  checkgroupMapper.selectAll();
    }
    /**
     * @Author: 15041
     * @Date: 14:44 2021/1/11
     * @Parms: [checkGroup]
     * @ReturnType: com.itheima.health.pojo.CheckGroup
     *Description: 根据传递过来的信息查询单个检查组数据
    */
    @Override
    public CheckGroup findById(CheckGroup checkGroup) {
        log.info("检查组-查询单个检查组信息传参：{}",checkGroup);
        return checkgroupMapper.selectOne(checkGroup);
    }

    /**
     * @Author: 15041
     * @Date: 12:13 2021/1/6
     * @Parms: [id]
     * @ReturnType: void
     *Description: 根据id删除检查组 判断是否存在检查项
    */
    @Override
    @Transactional(rollbackFor = Exception.class)//添加事务 如果出现异常回滚
    public void delete(Integer id) {
        log.info("检查组-根据id删除serviceImpl层方法传参："+id);
        //创建关联表对象
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        //传入要删除的检查组id
        checkGroupItem.setCheckgroupId(id);
        //根据检查组id查询检查组下的子检查组数量
        int count = checkGroupItemMapper.selectCount(checkGroupItem);
        log.info("统计检查组下的子检查组数量："+count);
        //如果大于0证明还有子检查组，自定义异常声明无法删除
        if (count>0){
            throw new BusinessException("该检查组还有子检查组不能删除！！");
        }
        //如果小于0证明没有子检查组，可以删除
        checkgroupMapper.deleteByPrimaryKey(id);
    }


}
